package org.datascooter.impl;

import java.util.Date;
import java.util.UUID;

import org.datascooter.inface.IData;

/**
 * Common abstract ancestor with common functionality and fields for descendants which must be saved into database. Not required for all objects but very usefull. Thanks to UUID it is guarantied of
 * identity everywhere - it may be useful if you want to identify items from different remote sources
 * 
 * @author nemo
 * 
 */
public abstract class Data implements IData<String> {

	private static final long serialVersionUID = 3507486395692272085L;
	protected String id;
	protected Date created;

	protected Data() {
	}

	/**
	 * Returns unique UUID string as unuque identificator.
	 */
	public String getId() {
		if (id == null) {
			id = UUID.randomUUID().toString();
		}
		return id;
	}

	public void rebuildId() {
		setId(UUID.randomUUID().toString());
	}

	public void setId(String id) {
		this.id = id;
	}

	public String toString() {
		return id;
	}

	public int hashCode() {
		return getId().hashCode();
	}

	public boolean equals(Object object) {
		if (!(object instanceof Data)) {
			return false;
		}
		if (this.getClass() != object.getClass()) {
			return false;
		}
		Data data = (Data) object;
		return getId().equals(data.getId());
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}
}
